/* eslint-disable no-unused-vars */
/* eslint-disable no-this-before-super */
import namespace from "./namespace";

// import Snap from "snapsvg";
import Raphael from "raphael";
import JSONUtil from "./util/JSONUtil";
import extend from "lodash/extend";

class VectorFigure extends namespace.Figure {
  NAME = `${namespace.namespace}.VectorFigure`;

  constructor(attr, setter = {}, getter = {}) {
    super();

    this.setterWhitelist = extend(
      {
        radius: function (radius) {
          return this.setRadius(radius);
        },
        bgColor: function (color) {
          return this.setBackgroundColor(color);
        },
        strokeWidth: function (w) {
          return this.setStrokeWidth(w);
        },
        stroke: function (stroke) {
          return this.setStroke(stroke);
        }
      },
      this.setterWhitelist,
      setter
    );

    this.getterWhitelist = extend(
      {
        radius: function () {
          return this.getRadius();
        },
        bgColor: function () {
          return this.getBackgroundColor();
        },
        strokeWidth: function () {
          return this.getStrokeWidth();
        },
        stroke: function () {
          return this.getStroke();
        }
      },
      this.getterWhitelist,
      getter
    );

    this.stroke = Raphael.getRGB("#303030");
    this.bgColor = Raphael.getRGB("#fff");
    this.radius = 0;
    this.strokeWidth = 1;

    super.attr(attr);
  }

  setRadius(radius) {
    this.radius = radius;
    this.repaint();
    return this;
  }

  getRadius() {
    return this.radius;
  }

  repaint(attributes) {
    if (this.repaintBlocked === true || this.shape === null) {
      return;
    }

    attributes = attributes || {};

    attributes.x = this.getAbsoluteX();
    attributes.y = this.getAbsoluteY();

    JSONUtil.ensureDefault(attributes, "stroke-width", this.strokeWidth);
    JSONUtil.ensureDefault(attributes, "stroke", this.stroke.hex);
    JSONUtil.ensureDefault(attributes, "fill", this.bgColor.hex);

    super.repaint(attributes);

    return this;
  }

  setBackgroundColor(color) {
    this.bgColor = Raphael.getRGB(color);
    this.repaint();

    return this;
  }

  setBgColor(color) {
    return this.setBackgroundColor(color);
  }

  getBackgroundColor() {
    return this.bgColor.hex;
  }

  setStroke(color) {
    this.stroke = Raphael.getRGB(color);
    this.repaint();

    return this;
  }

  getStroke() {
    return this.stroke.hex;
  }

  setStrokeWidth(w) {
    this.strokeWidth = w;
    this.repaint();

    return this;
  }

  getStrokeWidth() {
    return this.strokeWidth;
  }

  /**
   * @inherit
   */
  getPersistentAttrs() {
    return extend(super.getPersistentAttrs(), {
      bgColor: this.bgColor.hex,
      stroke: this.stroke.hex,
      strokeWidth: this.strokeWidth,
      radius: this.radius
    });
  }

  /**
   * @inherit
   */
  setPersistentAttrs(figure) {
    super.setPersistentAttrs(figure);

    if (typeof figure.radius !== "undefined") {
      this.setRadius(figure.radius);
    }

    if (typeof figure.bgColor !== "undefined") {
      this.setBackgroundColor(figure.bgColor);
    }

    if (typeof figure.strokeWidth !== "undefined") {
      this.setStrokeWidth(figure.strokeWidth);
    }

    if (typeof figure.stroke !== "undefined") {
      this.setStroke(figure.stroke);
    }

    return this;
  }
}

namespace.VectorFigure = VectorFigure;
